package com.easyadmin.controller;

import com.easyadmin.annotation.GlobalInterceptor;
import com.easyadmin.annotation.VerifyParam;
import com.easyadmin.entity.enums.OperModuleEnum;
import com.easyadmin.entity.enums.PermissionCodeEnum;
import com.easyadmin.entity.enums.ResponseCodeEnum;
import com.easyadmin.entity.po.SysTask;
import com.easyadmin.entity.query.SysTaskQuery;
import com.easyadmin.entity.query.SysTaskRunLogQuery;
import com.easyadmin.entity.vo.AjaxResponseVO;
import com.easyadmin.entity.vo.PaginationResult;
import com.easyadmin.exception.BusinessException;
import com.easyadmin.service.SysTaskRunLogService;
import com.easyadmin.service.SysTaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * 任务调度 控制层
 */
@RestController
@RequestMapping("/api/task")
public class SysTaskController extends ABaseController {

    private static final Logger logger = LoggerFactory.getLogger(SysTaskController.class);

    @Resource
    private SysTaskService sysTaskService;

    @Resource
    private SysTaskRunLogService sysTaskRunLogService;

    @RequestMapping("loadTaskList")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.TASK_LIST)
    public AjaxResponseVO loadTaskList(SysTaskQuery taskQuery) {
        try {
            taskQuery.setOrderBy("task_id desc");
            PaginationResult<SysTask> taskList = sysTaskService.findListByPage(taskQuery);
            return getSuccessResponseVO(taskList);
        } catch (Exception e) {
            logger.error("获取任务信息异常", e);
            return getServerErrorResponseVO();
        }
    }

    /**
     * @Description: 保存任务调度
     * @auther: 多多洛
     * @date: 10:31 2020/12/4
     * @param: [sysTask, immediatelyRun]
     * @return: AjaxResponseVO
     */
    @RequestMapping("saveTask")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.TASK_EDIT, opObject = OperModuleEnum.OBJECT_TASK, opDescription = "保存任务调度,任务调度信息:#{#param0}")
    public AjaxResponseVO saveTask(@VerifyParam SysTask sysTask, Boolean immediatelyRun) {
        try {
            sysTaskService.saveTask(sysTask, immediatelyRun);
            return getSuccessResponseVO(null);
        } catch (BusinessException e) {
            logger.error("任务保存失败", e);
            return getBusinessErrorResponseVO(e);
        } catch (Exception e) {
            logger.error("任务保存失败", e);
            return getServerErrorResponseVO();
        }
    }

    /**
     * @Description: 开启，停用  任务
     * @auther: 多多洛
     * @date: 10:33 2020/12/4
     * @param: [taskId, opStatus]
     * @return: AjaxResponseVO
     */
    @RequestMapping("suspendAndStartTask")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.TASK_EDIT, opObject = OperModuleEnum.OBJECT_TASK, opDescription = "#{#param1==0?'暂停':'开启'}任务调度，任务ID" +
            ":#{#param0}")
    public AjaxResponseVO suspendAndStartTask(Integer taskId, Integer opStatus) {
        try {
            sysTaskService.suspendAndStartTask(taskId, opStatus);
            return getSuccessResponseVO(null);
        } catch (BusinessException e) {
            logger.error("停用开启任务失败", e);
            return getBusinessErrorResponseVO(e);
        } catch (Exception e) {
            logger.error("停用开启任务失败", e);
            return getServerErrorResponseVO();
        }
    }

    /**
     * @Description: 删除任务
     * @auther: 多多洛
     * @date: 10:34 2020/12/4
     * @param: [taskId]
     * @return: AjaxResponseVO
     */
    @RequestMapping("delTask")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.TASK_EDIT, opObject = OperModuleEnum.OBJECT_TASK, opDescription = "删除任务调度,任务Id:#{#param0}")
    public AjaxResponseVO delTask(Integer taskId) {
        try {
            sysTaskService.deleteTask(taskId);
            return getSuccessResponseVO(null);
        } catch (BusinessException e) {
            logger.error("删除任务失败", e);
            return getBusinessErrorResponseVO(e);
        } catch (Exception e) {
            logger.error("删除任务失败", e);
            return getServerErrorResponseVO();
        }
    }

    /**
     * @Description: 立即执行任务
     * @auther: 多多洛
     * @date: 10:35 2020/12/4
     * @param: [taskId]
     * @return: AjaxResponseVO
     */
    @RequestMapping("immediatelyRunTask")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.TASK_EDIT, opObject = OperModuleEnum.OBJECT_TASK, opDescription = "立即执行任务,任务Id:#{#param0}")
    public AjaxResponseVO immediatelyRunTask(Integer taskId) {
        try {
            sysTaskService.immediatelyRunTask(taskId);
            return getSuccessResponseVO(null);
        } catch (BusinessException e) {
            logger.error("立即执行任务失败", e);
            return getBusinessErrorResponseVO(e);
        } catch (Exception e) {
            logger.error("立即执行失败", e);
            return getServerErrorResponseVO();
        }
    }

    /**
     * 查看调度日志
     * @param query
     * @return
     */
    @RequestMapping("loadRunTaskLog")
    @GlobalInterceptor(permissionCode = PermissionCodeEnum.TASK_LOG)
    public AjaxResponseVO loadRunTaskLog(SysTaskRunLogQuery query) {
        try {
            if (query.getTaskId() == null) {
                throw new BusinessException(ResponseCodeEnum.CODE_600);
            }
            query.setOrderBy("id desc");
            return getSuccessResponseVO(sysTaskRunLogService.findListByPage(query));
        } catch (BusinessException e) {
            logger.error("获取执行日志失败", e);
            return getBusinessErrorResponseVO(e);
        } catch (Exception e) {
            logger.error("获取执行日志失败", e);
            return getServerErrorResponseVO();
        }
    }
}